﻿﻿	require.config({
		  urlArgs: "version=20170407",
		  paths: {
		    'jquery': '/js/packages/jquery-2.14-min',
		    'jquery_page':"/js/packages/jquery.twbsPagination",
		    'bootstrap': '/bootstrap/js/bootstrap.min',
		    'bootstrapdate': '/bootstrap/bootstrap-date/js/bootstrap-datetimepicker.min',
		    
		  },
		  shim: {
		    'bootstrap': {
		      deps: ['jquery'],
		      exports: 'bootstrap'
		    },
		  	'bootstrapdate':{
		  		deps: ['jquery'],
		        exports: 'bootstrap'
		  	},
		  	'jquery_page':{
		  		deps: ['jquery'],
		        exports: 'bootstrap'
		  	}
		  }
		  
		});

beforeValue = [];	
function createTag(tagid, content, type){
		return '<div style="float:left">' 
			+'<div id="tagid_'+ tagid +'" style="margin:1px 2px">'
			+ '<div class="label '+ getLabelType(type) +'" id="label_'+tagid+'">'+content
			+ ' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="toValue(\''+tagid+'\')"></div>'
			+ '<div class="glyphicon glyphicon-remove" aria-hidden="true" onclick="removeTag(\''+tagid+'\')" ></div></div></div>'
			+ ' </div> '
	}
	
	//新技术分支\新技术功效标签
	function createNewTag(tagid,type){//add a tag doesn't exit
		var t = 0;
		var promp_string = '';
		if(type=='branch'){
			t = 0;
			promp_string='新技术分支'
		}else if (type == 'effect'){
			t = 1;
			promp_string='新技术功效'
		}
		return ' <div style="float:right"> <div id="ntagid_' + tagid + '" style="margin:1px 2px">'
		+ '<div class="label '+getLabelType(type)+'" id="nlabel_'+tagid +'">'+promp_string
		+ ' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="toNewValue(\''
		+ tagid +'\','+t+')"></div> </div></div>'+'</div></div>'
	}
	function getLabelType(type){
		if(type == 'branch')
			return 'label-primary';
		else if(type=='effect')
			return 'label-success'
		else return ''; 
	}
	function formatTag(id, str, type){
		id = type+"_"+id
		var label_type = getLabelType(type)
		var htmlresult = '';
		//no keywords exist
		if(str.length<1){
			htmlresult = '<div style="float:center">'
				+ '<div id="tag_'+id+'">' 
				+ htmlresult+ '</div>'
				
			//console.log(htmlresult+createNewTag(id,type))
			return htmlresult + createNewTag(id,type);
		}
		var strs = str.split("#");
		
		
		for(var s in strs){
			//console.log(atag);
			htmlresult = htmlresult + createTag(id+'_'+s, strs[s], type);
		}
		//console.log('**'+htmlresult+'\n')
		htmlresult = '<div style="float:center">'
			+ '<div id="tag_'+id+'">' 
			+ htmlresult+ '</div>'
			
		//console.log(htmlresult+createNewTag(id,type))
		return htmlresult + createNewTag(id,type);
		//console.log(tagid)
		//$('#'+tagid).remove();
	}
	
function toValue(tagid){
		//alert($('#id_h3').text())
		//alert($('#id1').html())
		//console.log($('#label_'+tagid).text());
		var res = $('#label_'+tagid).text().trim();
		tmp =  {
			value : res,
			tagid : tagid
		}
		console.log(tagid)
		beforeValue.push(tmp)
		console.log(beforeValue);
		$('#label_'+tagid).empty();
		$('#label_'+tagid).append('<input id="input_'+tagid+'_value" type="text" style="color:black" value="'+res+'" /> <div class="glyphicon glyphicon-ok" aria-hidden="true" onclick="toLabel(\''+tagid+'\')"/> <div class="glyphicon glyphicon-remove" aria-hidden="true" onclick="removeTag(\''+tagid+'\')"/> </div>')

		//console.log($('#id1').value());
	}


	function toLabel(tagid){
		//console.log($('#id1').html());
		//console.log($('#id_value_1').html());
		//console.log($('#input_'+tagid+'_value').val());
		var res = $('#input_'+tagid+'_value').val().trim();
		//console.log($('#aa').val());
		$('#label_'+tagid).empty();	//$('#id1').remove();
		$('#label_'+tagid).append(res+' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="toValue(\''+tagid+'\')" /> <div class="glyphicon glyphicon-remove" aria-hidden="true" onclick="removeTag(\''+tagid+'\')" />');
		
		console.log('yes')
		obj = null;
		for(var o in beforeValue){
			//console.log(tagid+'a   '+beforeValue[o].tagid+'a')
			if(beforeValue[o].tagid==tagid){
				console.log(beforeValue[o].value+'  '+res)
				if(beforeValue[o].value==res) return;
				$.ajax({
					url:'/bref/update',
				    type:'GET', //GET
				    async:false,    //或false,是否异步
				    data:{
				    	id : tagid,
				    	beforevalue : beforeValue[o].value,
				    	currentvalue : res,
				    },
				    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
				    success:function(data,textStatus,jqXHR){
				        //console.log(data)		          
				    },
				});
				beforeValue.splice(o,1);
				return;
			}
		}
		//$('#id1').html($('#id1').val()+' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="getValue()"></div> <div class="glyphicon glyphicon-remove" aria-hidden="true"></div>')
	}
	function toNewValue(tagid,type){
		//alert($('#id_h3').text())
		//alert($('#id1').html())
		//console.log($('#nlabel_'+tagid).text());
		var res = $('#nlabel_'+tagid).text().trim();
		$('#nlabel_'+tagid).empty();
		$('#nlabel_'+tagid).append(
				'<input id="ninput_' + tagid + '_value" type="text" style="color:black" value="'+res+'" />' 
				+'<div class="glyphicon glyphicon-ok" aria-hidden="true" onclick="toNewBranchLabel(\''+tagid+'\')"/>' 
				+'<div class="glyphicon glyphicon-remove" aria-hidden="true" onclick="toNewLabel(\''+tagid+'\','+type+')"/> </div>')

		//console.log($('#id1').value());
	}
	function toNewLabel(tagid,type){
		//console.log($('#id1').html());
		//console.log($('#id_value_1').html());
		//console.log($('#ninput_'+tagid+'_value').val());
		var res = $('#ninput_'+tagid+'_value').val().trim();
		//console.log($('#aa').val());
		$('#nlabel_'+tagid).empty();	//$('#id1').remove();
		$('#nlabel_'+tagid).append('新技术分支'
				+' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="toNewValue(\''+tagid+'\','+type+')" />');

		//$('#id1').html($('#id1').val()+' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="getValue()"></div> <div class="glyphicon glyphicon-remove" aria-hidden="true"></div>')
	}
	function toNewBranchLabel(tagid){
		//console.log($('#id1').html());
		//console.log($('#id_value_1').html());
		//console.log($('#ninput_'+tagid+'_value').val());
		var res = $('#ninput_'+tagid+'_value').val().trim();
		//console.log($('#aa').val());
		$('#nlabel_'+tagid).empty();	//$('#id1').remove();
		$('#nlabel_'+tagid).append('新技术分支'
				+' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="toNewValue(\''+tagid+'\')" />');
		//console.log('#tag_'+tagid)
		var id = tagid+'_'+$('#tag_'+tagid).children().length;
		id = id.substring(id.indexOf('_')+1);
		var type=''
		if(tagid.indexOf('branch')!=-1)
			type='branch';
		else if(tagid.indexOf('effect')!=-1)
			type = 'effect'
		$('#tag_'+tagid).append(createTag(id,res,type));
		$.ajax({
			url:'/bref/newValue',
		    type:'GET', //GET
		    async:false,    //或false,是否异步
		    data:{
		    	id : tagid,
		    	currentvalue : res,
		    },
		    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
		    success:function(data,textStatus,jqXHR){
		        //console.log(data)		          
		    },
		});
		
		
		
		
		
		//$('#id1').html($('#id1').val()+' <div class="glyphicon glyphicon-edit" aria-hidden="true" onclick="getValue()"></div> <div class="glyphicon glyphicon-remove" aria-hidden="true"></div>')
	}


	function removeTag(tagid){
		//console.log(tagid)
		var res = $('#label_'+tagid).text().trim();
		//console.log(res)
		$.ajax({
					url:'/bref/remove',
				    type:'GET', //GET
				    async:false,    //或false,是否异步
				    data:{
				    	id : tagid,
				    	currentvalue : res,
				    },
				    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
				    success:function(data,textStatus,jqXHR){
				        //console.log(data)		          
				    },
				});
		$('#tagid_'+tagid).parent().remove();
		
	}
	

require(['jquery','bootstrap','jquery_page'], function ($) {
	//存储要和不要

	var curr_page = 1;
	var curr_size = 20;
	var total_page;
	loadpage(curr_page,curr_size);

	
	function loadpage(pageNumber,pageSize) {
		console.log("load result");
		//添加通信机制、更新检索信息		
		
		$.ajax({
			url:'/document/getTechEff',
		    type:'GET', //GET
		    async:false,    //或false,是否异步
		    data:{
		    	pageNumber:pageNumber,
		    	pageSize:pageSize,
		    	//depotId:2
		    	//perSize:perSize
		    },
		    //timeout:5000,    //超时时间
		    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
		    /*beforeSend:function(xhr){
		        console.log(xhr)
		        console.log('发送前')
		    }*/
		    success:function(data,textStatus,jqXHR){
		    	//if(data==undefined) console.log('no data')
		    	console.log(data.totalSize)
		    	if(data.entries.length==0){
		    		console.log('not found')
		    		$('#no-result-div').prop('style',"display:block")
		    	}else{
		    		$('#search-result-div').prop('style',"display:block")
		    		showSearch(data.entries,data.page,data.pageSize);
		    		show_pages(data.page,data.totalPages);
		    	}
		    	
		        //console.log(data)
		        //console.log(textStatus)
		        //console.log(jqXHR)
		    },
		    error:function(xhr,textStatus){
		        //console.log('什么错误')
		        //console.log(xhr.responseText)
		        //console.log(textStatus)
		    },
		    complete:function(){
		        //console.log('结束')
		    }
		});
	}
	
	function showSearch(data,page,pageSize) {
		$('.chooseall').prop("checked", false);
		$('#show-table')[0].innerHTML="";
		for(var o in data){
			//console.log(data[o].id)
			var dataObj=data[o].patent;//eval("("+data[o]+")");
			var order = 1+parseInt(o)+(parseInt(page)-1)*parseInt(pageSize)
			//console.log(data)
			addtd(order,data[o].id,data[o],'show-table');
			//addtd(parseInt(data[o].id),dataObj,parseInt(data[o].score),'show-table');
		}
		//showSelected();
	}
	function addtd(order,o,dataObj,idid){
		var tr = document.createElement("tr");
		tr.setAttribute('id','tr-'+o);
		
		var div_start = "<div class = 'table-content'>";

		var div_abs = document.createElement("div");
		div_abs.setAttribute("class","table-content");
		div_abs.setAttribute("id","abs-"+o);
		
		var div_right = document.createElement("div");
		div_right.setAttribute("class","table-content");
		div_right.setAttribute("id","right-"+o);
		
		var div_end = "</div>";
		
		var td1 = document.createElement("td");
		td1.setAttribute("class","show-column1");
		td1.innerHTML=order;
		var td2 = document.createElement("td");
		td2.setAttribute("class","show-column2");
		td2.innerHTML=div_start+dataObj.claim+div_end;
		var td3 = document.createElement("td");
		td3.setAttribute("class","show-column3");
		var abstraction = dataObj.abstraction.split("|")[0];
		td3.innerHTML="<div class = 'table-content'>"+"<h5>"+abstraction+"</h5>"+div_end;
		var td4 = document.createElement("td");
		td4.setAttribute("class","show-column4");
		//td4.innerHTML=div_start+dataObj.branch+div_end;
		td4.innerHTML = formatTag(o,dataObj.branch,'branch');
		
		var td5 = document.createElement("td");
		td5.setAttribute("class","show-column5");
		//td5.innerHTML=div_start+dataObj.effect+div_end;
		td5.innerHTML = formatTag(o,dataObj.effect,'effect');


		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);
		tr.appendChild(td5);
		
		$('#'+idid).append(tr);
		//
	}
	
	function show_pages(page,totalPages) {
		//console.log("page")
		//console.log(page,totalPages)
		$("#bref-page").twbsPagination({
			onPageClick:function pageselectCallback(event,page_index){
				loadpage(page_index,curr_size)
			},
			initiateStartPageClick:false,
			totalPages:totalPages,
			visiblePages:5,
			first:'首页',
			last:'尾页',
			prev:'上一页',
			next:'下一页',
	    });
	}


});
